package com.gensdai.leliang.dialog;

import android.app.Activity;
import android.content.Context;
import android.net.Uri;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.facebook.drawee.view.SimpleDraweeView;
import com.gensdai.leliang.R;
import com.gensdai.leliang.entity.parseBean.ProductAttributesListBean;
import com.gensdai.leliang.popupwindow.BasePopupWindow;
import com.gensdai.leliang.utils.BaseUtils;
import com.gensdai.leliang.view.AmountView;
import com.gensdai.leliang.view.flowtag.FlowTagLayout;
import com.gensdai.leliang.view.flowtag.OnInitSelectedPosition;
import com.gensdai.leliang.view.flowtag.OnTagSelectListener;

import java.util.ArrayList;
import java.util.List;

/**
 * 选择商品属性的dialog
 * Created by Administrator on 2017/5/27.
 */

public class DialogPopup extends BasePopupWindow implements View.OnClickListener {

    SimpleDraweeView img;

    ImageView closeBtn;                    //关闭按钮
    TextView price;                    //价格
    TextView stockNum;              //库存text
    TextView selectTextView;        //已选择的textview
    TextView pay;                       //立即购买
    TextView payCart;                   //加入购物车
    AmountView amountViewText;                   //右下角增加减少按钮

    private View popupView;

    FlowTagLayout taglayout;
    ProductAttributesListBean currentBean = null;
    private List<ProductAttributesListBean> mVals = null;
    private int currentNumber = 1;

    private String titleImage = "";    //显示在这个pop上的image



    public OnPayItemClick click;

    public DialogPopup(Activity context) {
        super(context);
    }

    public void setOnPayItemClick(OnPayItemClick click){
        this.click = click;
    }

    public void setData(List<ProductAttributesListBean> data, String image) {
        mVals = data;
        titleImage = image;
        bindEvent();
    }

    //初始化
    private void bindEvent() {
        if (popupView != null) {
            img = (SimpleDraweeView) popupView.findViewById(R.id.productIcon);
            closeBtn = (ImageView) popupView.findViewById(R.id.closeBtn);
            price = (TextView) popupView.findViewById(R.id.price);
            stockNum = (TextView) popupView.findViewById(R.id.stockNum);
            selectTextView = (TextView) popupView.findViewById(R.id.selectLable);
            amountViewText = (AmountView) popupView.findViewById(R.id.amount_view);
            /*test*/
            taglayout = (FlowTagLayout) popupView.findViewById(R.id.size_flow_layout);
            TagAdapters mSizeTagAdapter = new TagAdapters<>(getContext());
            taglayout.setTagCheckedMode(FlowTagLayout.FLOW_TAG_CHECKED_SINGLE);
            taglayout.setAdapter(mSizeTagAdapter);
            taglayout.setOnTagSelectListener(new OnTagSelectListener() {
                @Override
                public void onItemSelect(FlowTagLayout parent, List<Integer> selectedList) {
                    if (selectedList != null && selectedList.size() > 0) {
                        for (int i : selectedList) {
                            ProductAttributesListBean b = (ProductAttributesListBean)parent.getAdapter().getItem(i);
                            changeTitleLayout(b);
                        }
                    }else{
                        currentBean = null;
                        selectTextView.setText("未选择商品分类");
                    }
                }
            });

            mSizeTagAdapter.onlyAddAll(mVals);
            /*test*/

            pay = (TextView) popupView.findViewById(R.id.pay);
            payCart = (TextView) popupView.findViewById(R.id.payCart);
            pay.setOnClickListener(this);
            payCart.setOnClickListener(this);


            img.setImageURI(Uri.parse(titleImage));
            TextView nowPayBtn = (TextView) popupView.findViewById(R.id.pay);

            closeBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dismiss();
                }
            });

            if(mVals.size() > 0){
                changeTitleLayout(mVals.get(0));
            }


            amountViewText.setOnAmountChangeListener(new AmountView.OnAmountChangeListener() {
                @Override
                public void onAmountChange(View view, int amount) {
                    currentNumber = amount;
                    change();
                }
            });

        }
    }


    @Override
    public View onCreatePopupView() {
        popupView = LayoutInflater.from(getContext()).inflate(R.layout.dialog_select_property, null);
        return popupView;
    }

    @Override
    public View initAnimaView() {
        return popupView.findViewById(R.id.poplayout);
    }

    @Override
    protected Animation initShowAnimation() {
        return getTranslateAnimation(250 * 5, 0, 260);
    }

    @Override
    protected Animation initExitAnimation() {
        return getTranslateAnimation(0, 250 * 5, 260);
    }

    @Override
    public View getClickToDismissView() {
        return getPopupWindowView();
    }



    //改变view
    private void changeTitleLayout(ProductAttributesListBean bean) {
        currentBean = bean;
        if(amountViewText != null && bean.getStockNum() != null&& !"null".equalsIgnoreCase(bean.getStockNum()))
            amountViewText.setGoods_storage((int)Float.parseFloat(bean.getStockNum()));
        price.setText(String.format(getContext().getString(R.string.product_price), bean.getPrice()));
        stockNum.setText(String.format(getContext().getString(R.string.product_stocknum), bean.getStockNum()));
        selectTextView.setText(String.format(getContext().getString(R.string.product_selecttext), bean.getAttributeName()));
        //改变产品图
        img.setImageURI(Uri.parse(bean.getAttPic()));
        change();
    }

    private void change() {
        if (currentBean != null) {
            Float countPrice = Float.valueOf(currentBean.getPrice()) /** currentNumber*/;
            price.setText(String.format(getContext().getString(R.string.product_price), String.valueOf(BaseUtils.formatFloat(countPrice))));

        }
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.payCart) {  //加入到购物车

            if(click != null){
                if (currentBean != null) {
                    if(Integer.parseInt(currentBean.getStockNum()) <= 0){
                        Toast.makeText(getContext(), "该分类库存不足，请选择其他分类", Toast.LENGTH_SHORT).show();
                        return;
                    }
                    click.OnPayCartClick(currentBean,currentNumber);
                }else{
                    Toast.makeText(getContext(), "请选择商品分类", Toast.LENGTH_SHORT).show();
                }
            }
        } else if (v.getId() == R.id.pay) {  //立即购买按钮

            if(click != null){

                if (currentBean != null) {
                    if(Integer.parseInt(currentBean.getStockNum()) <= 0){
                        Toast.makeText(getContext(), "该分类库存不足，请选择其他分类", Toast.LENGTH_SHORT).show();
                        return;
                    }
                    click.OnPayClick(currentBean,currentNumber);
                }else{
                    Toast.makeText(getContext(), "请选择商品分类", Toast.LENGTH_SHORT).show();
                }
            }
        }
    }

    public interface OnPayItemClick{
        void OnPayClick(ProductAttributesListBean currentBean,int number);
        void OnPayCartClick(ProductAttributesListBean currentBean,int number);
    }


    public static class TagAdapters<T> extends BaseAdapter implements OnInitSelectedPosition {

        private final Context mContext;
        private final List<T> mDataList;

        public TagAdapters(Context context) {
            this.mContext = context;
            mDataList = new ArrayList<>();
        }

        @Override
        public int getCount() {
            return mDataList.size();
        }

        @Override
        public Object getItem(int position) {
            return mDataList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            View view = LayoutInflater.from(mContext).inflate(R.layout.tag_item, null);

            TextView textView = (TextView) view.findViewById(R.id.tv_tag);
            T t = mDataList.get(position);

            if (t instanceof ProductAttributesListBean) {
                textView.setText(((ProductAttributesListBean) t).getAttributeName());
            }
            return view;
        }

        public void onlyAddAll(List<T> datas) {
            mDataList.addAll(datas);
            notifyDataSetChanged();
        }

        public void clearAndAddAll(List<T> datas) {
            mDataList.clear();
            onlyAddAll(datas);
        }

        @Override
        public boolean isSelectedPosition(int position) {
            if (position % 2 == 0) {
                return true;
            }
            return false;
        }
    }
}
